package 树.遍历;

import 抽象数据类型.TreeNode;

import java.util.LinkedList;
import java.util.Stack;

public class MiddleTraverse {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(7);
        middleOrderTraverse(root);
        for (Object o : list) {
            System.out.println(((TreeNode) o).getVal());
        }

    }

    //中序遍历
    public static void middleOrderTraverse(TreeNode node) {
        System.out.println("\n中序遍历");
        if (node == null) {
            return;
        }
        Stack stack = new Stack();

        TreeNode root = node;
        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                root = root.getLeft();
            }
            if (!stack.isEmpty()) {
                root = (TreeNode) stack.pop();
                System.out.print(root.val);
                root = root.getRight();
            }
        }
    }    //树的深度
}
